//
//  CalendarCell.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 2017/11/19.
//  Copyright © 2017年 com.xtion.crm. All rights reserved.
//

#import "CalendarCell.h"
#import "JTCalendar.h"

@class StudentInfo;

@interface CalendarCell()<JTCalendarDelegate>
{
    JTCalendarMenuView *_calendarMenuView;
    JTHorizontalCalendarView *_calendarContentView;
    
    JTCalendarManager *_calendarManager;
    
    NSMutableDictionary *_eventsByDate_ChiDao;
    NSMutableDictionary *_eventsByDate_ZaoTui;
    
    NSDate *_todayDate;
    NSDate *_minDate;
    NSDate *_maxDate;
    
    NSDate *_dateSelected;
}
@end

@implementation CalendarCell

-(id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    self.backgroundColor = [UIColor clearColor];
    
    return self;
}

-(void)setDate:(NSDate *)date
{
    _todayDate = date;
    _dateSelected = date;
    
    [self initVariable];
    [self initUI];
}

-(void)initVariable
{
    _eventsByDate_ChiDao = [NSMutableDictionary new];
    _eventsByDate_ZaoTui = [NSMutableDictionary new];
}

-(void)initUI
{
    _calendarMenuView = [[JTCalendarMenuView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    //[self.contentView addSubview:_calendarMenuView];
    
    _calendarContentView = [[JTHorizontalCalendarView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
    [self.contentView addSubview:_calendarContentView];
    
    _calendarManager = [JTCalendarManager new];
    _calendarManager.delegate = self;
    
    // Create a min and max date for limit the calendar, optional
    [self createMinAndMaxDate];
    
    [_calendarManager setMenuView:_calendarMenuView];
    [_calendarManager setContentView:_calendarContentView];
    [_calendarManager setDate:_todayDate];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    _calendarMenuView.centerX = self.contentView.centerX;
    [_calendarMenuView setMenuViewTextColor:[UIColor redColor]];
    
    //_calendarContentView.top = _calendarMenuView.bottom;
    _calendarContentView.width = self.width;
    _calendarContentView.height = self.height;
}

#pragma mark - CalendarManager delegate

// Exemple of implementation of prepareDayView method
// Used to customize the appearance of dayView
- (void)calendar:(JTCalendarManager *)calendar prepareDayView:(JTCalendarDayView *)dayView
{
    // Today
    if([_calendarManager.dateHelper date:[NSDate date] isTheSameDayThan:dayView.date]){
        dayView.circleView.hidden = YES;
        dayView.textLabel.textColor = [UIColor redColor];
        dayView.chineseTextLabel.textColor = [UIColor redColor]; 
    }
    // Selected date
    else if(_dateSelected && [_calendarManager.dateHelper date:_dateSelected isTheSameDayThan:dayView.date]){
        dayView.circleView.hidden = YES;
        dayView.backgroundColor = [UIColor lightGrayColor];
    }
    // Other month
    else if(![_calendarManager.dateHelper date:_calendarContentView.date isTheSameMonthThan:dayView.date]){
        dayView.circleView.hidden = YES;
        dayView.dotView.backgroundColor = [UIColor redColor];
        dayView.chineseTextLabel.textColor = [UIColor lightGrayColor];
    }
    // Another day of the current month
    else{
        dayView.circleView.hidden = YES;
        dayView.dotView.backgroundColor = [UIColor redColor]; 
        dayView.chineseTextLabel.textColor = [UIColor blackColor];
    }
    
    //自定义圆点
    dayView.dotView.hidden = YES;
    CGFloat dayWidth = [UIDevice screenWidth]/7.0;//一周七天
    if([self haveEventForDay:dayView.date dic:_eventsByDate_ChiDao]){
        UIView *dotView  = [self dotView:dayView];
        dotView.backgroundColor = [UIColor redColor];
        
        dotView.top = 10;
        dotView.right = dayWidth - 2;
    }
    if([self haveEventForDay:dayView.date dic:_eventsByDate_ZaoTui]){
        UIView *dotView  = [self dotView:dayView]; 
        dotView.backgroundColor = [UIColor greenColor];
        
        dotView.top = 25;
        dotView.right = dayWidth - 1;
    }
}

-(UIView*)dotView:(UIView*)dayView
{
    UIView *dotView = [UIView new];
    dotView.width = 8;
    dotView.height = 8;
    dotView.layer.cornerRadius = 4;
    dotView.layer.masksToBounds = YES;
    [dayView addSubview:dotView];
    
    dotView.backgroundColor = [UIColor redColor];
    
    dotView.layer.rasterizationScale = [UIScreen mainScreen].scale;
    dotView.layer.shouldRasterize = YES;
    
    return dotView;
}

- (void)calendar:(JTCalendarManager *)calendar didTouchDayView:(JTCalendarDayView *)dayView
{
    _dateSelected = dayView.date;
    [_calendarManager setDate:_dateSelected];
    
    // Animation for the circleView
    dayView.circleView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
    [UIView transitionWithView:dayView
                      duration:.3
                       options:0
                    animations:^{
                        dayView.circleView.transform = CGAffineTransformIdentity;
                        //Do Something
                        if([_delegate respondsToSelector:@selector(didTouchDayView:dateSelected:)])
                           [_delegate didTouchDayView:self dateSelected:_dateSelected];
                    } completion:nil];
    
    
    // Don't change page in week mode because block the selection of days in first and last weeks of the month
    if(_calendarManager.settings.weekModeEnabled){
        return;
    }
}

-(void)refreshCalendar:(JTCalendarDayView *)dayView
{
    // Load the previous or next page if touch a day from another month
    if(![_calendarManager.dateHelper date:_calendarContentView.date isTheSameMonthThan:dayView.date]){
        if([_calendarContentView.date compare:dayView.date] == NSOrderedAscending){
            [_calendarContentView loadNextPageWithAnimation];
        }
        else{
            [_calendarContentView loadPreviousPageWithAnimation];
        }
    }
}

- (void)calendar:(JTCalendarManager *)calendar prepareMenuItemView:(UIView *)menuItemView date:(NSDate *)date
{
    NSString *text = nil;
    
    if(date){
        text = [date toStringDate:@"yyyy年M月"];
        [(UILabel *)menuItemView setText:text];
        //[(UILabel *)menuItemView setTextColor:[UIColor whiteColor]];
        [(UILabel *)menuItemView setFont:[UIFont systemFontOfSize:14.0]];
    }
}

#pragma mark - CalendarManager delegate - Page mangement

// Used to limit the date for the calendar, optional
- (BOOL)calendar:(JTCalendarManager *)calendar canDisplayPageWithDate:(NSDate *)date
{
    return [_calendarManager.dateHelper date:date isEqualOrAfter:_minDate andEqualOrBefore:_maxDate];
}

- (void)calendarDidLoadNextPage:(JTCalendarManager *)calendar
{
    //    NSLog(@"Next page loaded");
    [_calendarMenuView setMenuViewTextColor:[UIColor redColor]];
    if([_delegate respondsToSelector:@selector(calendarDidLoadNextPage:dateSelected:)])
        [_delegate calendarDidLoadNextPage:self dateSelected:calendar.date];
}

- (void)calendarDidLoadPreviousPage:(JTCalendarManager *)calendar
{
    //    NSLog(@"Previous page loaded");
    [_calendarMenuView setMenuViewTextColor:[UIColor redColor]];
    if([_delegate respondsToSelector:@selector(calendarDidLoadPreviousPage:dateSelected:)])
        [_delegate calendarDidLoadPreviousPage:self dateSelected:calendar.date];
}

#pragma mark - Fake data
- (void)createMinAndMaxDate
{
    // Min date will be 2 month before today
    _minDate = [_calendarManager.dateHelper addToDate:_todayDate months:-6];
    
    // Max date will be 2 month after today
    _maxDate = [_calendarManager.dateHelper addToDate:_todayDate months:6];
}

// Used only to have a key for _eventsByDate
- (NSDateFormatter *)dateFormatter
{
    static NSDateFormatter *dateFormatter;
    if(!dateFormatter){
        dateFormatter = [NSDateFormatter new];
        dateFormatter.dateFormat = yMd;
    }
    
    return dateFormatter;
}

- (BOOL)haveEventForDay:(NSDate *)date dic:(NSMutableDictionary*)dic
{
    NSString *key = [[self dateFormatter] stringFromDate:date];
    
    if(dic[key] && [dic[key] count] > 0){
        return YES;
    }
    
    return NO;
}

- (void)createDateEvents:(NSArray*)upArr downArr:(NSArray*)downArr
{
    [_eventsByDate_ChiDao removeAllObjects];
    [_eventsByDate_ZaoTui removeAllObjects];
    
    for (NSInteger i = 0; i < upArr.count; i ++)
    {
        NSString *key = [[self dateFormatter] stringFromDate:upArr[i]];
        
        if(!_eventsByDate_ChiDao[key]){
            _eventsByDate_ChiDao[key] = [NSMutableArray new];
        }
        
        [_eventsByDate_ChiDao[key] addObject:upArr[i]];
    }
    
    for (NSInteger i = 0; i < downArr.count; i ++)
    {
        NSString *key = [[self dateFormatter] stringFromDate:downArr[i]];
        
        if(!_eventsByDate_ZaoTui[key]){
            _eventsByDate_ZaoTui[key] = [NSMutableArray new];
        }
        
        [_eventsByDate_ZaoTui[key] addObject:downArr[i]];
    }
    
    [self refreshCalendar];
}

-(void)refreshCalendar
{
    [_calendarManager reload];
}

+(CGFloat)caleHeight
{
    return 375;
}
@end
